package com.aacar.op.web;


import com.aacar.op.entity.img.Img;
import com.aacar.op.service.img.IImgService;
import com.aacar.op.service.security.SecUtils;
import com.aacar.op.utils.pojo.ActionResult;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统共用Controller
 */
@Controller
public class CommonsController {
    private static Logger logger = LoggerFactory.getLogger(CommonsController.class);

    @Autowired
    private IImgService imgService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, Model model) {
        model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName);
        return "login";
    }

    /**
     * <p>
     * 调用logout，任何现有的Session 都将会失效，而且任何身份都将会失去关联（例如，在Web 应用程序中，RememberMe cookie 也将被删除）。
     * 在Subject 注销后，该Subject 的实例被再次认为是匿名的。
     * 由于在Web 应用程序记住身份往往是依靠Cookies，然而Cookies 只能在Response 被committed 之前被删除，
     * 所以强烈建议在调用subject.logout()后立即将终端用户重定向到一个新的视图或页面。
     * 这样能够保证任何与安全相关的Cookies都能像预期的一样被删除。这是HTTP cookies 的功能限制，而不是Shiro的问题。
     * <p/>
     * <a href="http://blog.csdn.net/peterwanghao/article/details/8084126" >http://blog.csdn.net/peterwanghao/article/details/8084126</a>
     * </p>
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        Subject subject = SecUtils.getSubject();
        if (subject != null) {
            logger.debug("用户:{}注销成功", subject.getPrincipal());
            //手动注销 活动日志。

            subject.logout();
        }
        //重定向到登陆页
        return "redirect:/login";
    }


    /**
     * =================系统常见错误页面============*
     */

    @RequestMapping(value = "/404", method = {RequestMethod.GET})
    public String error404() {
        return "error/404";
    }

    @RequestMapping(value = "/403", method = {RequestMethod.GET})
    public String error403() {
        return "error/403";
    }

    @RequestMapping(value = "/401", method = {RequestMethod.GET})
    public String error401() {
        return "error/401";
    }

    @RequestMapping(value = "/500", method = {RequestMethod.GET})
    public String error500() {
        return "error/500";
    }


    /**
     * ==================图片上传接口=================
     */
    @RequestMapping(value = "/api/image/upload", method = RequestMethod.POST)
    @ResponseBody
    public ActionResult imageUpload(@RequestParam MultipartFile multipartFile,
                                    @RequestParam String type) {
        ActionResult actionResult;

        try {
            Img img = new Img();
            img.setType(type);
            img = imgService.uploadFile(img, multipartFile);
            actionResult = ActionResult.success("图片上传成功.");
            actionResult.addAttribute("img", img);
        } catch (Exception e) {
            logger.error("图片上传失败:{}", e);
            actionResult = ActionResult.failure("图片上传失败。");
        }
        return actionResult;
    }


}
